package com.test.resolver;

import javax.swing.plaf.PanelUI;
import java.util.Properties;

/**
 * 属性占位符解析器，用于解析字符串中的占位符并替换为相应的属性值
 */
public class PropertyPlaceholderResolver implements PlaceholderResolver{

    // 默认占位符前缀
    public static final String DEFAULT_PLACEHOLDER_PREFIX = "${";

    // 默认占位符后缀
    public static final String DEFAULT_PLACEHOLDER_SUFFIX = "}";

    // 默认值分隔符
    public static final String DEFAULT_VALUE_SEPARATOR = ":";

    // 占位符前缀
    private final String placeholderPrefix;
    // 占位符后缀
    private final String placeholderSuffix;
    // 默认值分隔符
    private final String valueSeparator;
    // 属性集合
    private final Properties properties;

    /**
     * 构造函数，使用默认的占位符前缀、后缀和默认值分隔符
     * @param properties 属性集合
     */
    public PropertyPlaceholderResolver(Properties properties){
        this(DEFAULT_PLACEHOLDER_PREFIX, DEFAULT_PLACEHOLDER_SUFFIX, DEFAULT_VALUE_SEPARATOR, properties);
    }

    /**
     * 构造函数，允许自定义占位符前缀、后缀和默认值分隔符
     * @param placeholderPrefix 占位符前缀
     * @param placeholderSuffix 占位符后缀
     * @param valueSeparator 默认值分隔符
     * @param properties 属性集合
     */
    public PropertyPlaceholderResolver(String placeholderPrefix, String placeholderSuffix,
                                       String valueSeparator, Properties properties){
        this.placeholderPrefix = placeholderPrefix;
        this.placeholderSuffix = placeholderSuffix;
        this.valueSeparator = valueSeparator;
        this.properties = properties;
    }

    /**
     * 解析字符串中的占位符并替换为相应的属性值
     * @param value 待解析的字符串
     * @return 解析后的字符串
     */
    @Override
    public String resolvePlaceholder(String value) {
        if (value == null || value.isEmpty() || !containsPlaceholder(value)){
            return value;
        }
        StringBuilder result = new StringBuilder(value);
        int startIndex = result.indexOf(placeholderPrefix);
        while (startIndex != -1){
            int endIndex = result.indexOf(placeholderSuffix, startIndex + placeholderPrefix.length());
            if (endIndex == -1){
                break;
            }
            String placeholder = result.substring(startIndex + placeholderPrefix.length(), endIndex);
            String defaultValue = null;

            int separatorIndex = placeholder.indexOf(valueSeparator);
            if (separatorIndex != -1){
                defaultValue = placeholder.substring(separatorIndex + valueSeparator.length());
                placeholder = placeholder.substring(0, separatorIndex);
            }
            String propVal = properties.getProperty(placeholder);
            if (propVal == null && defaultValue != null){
                propVal = defaultValue;
            }
            if (propVal != null){
                result.replace(startIndex, endIndex + placeholderSuffix.length(), propVal);
                startIndex = result.indexOf(placeholderPrefix, startIndex + propVal.length());
            }else {
                startIndex = result.indexOf(placeholderPrefix, endIndex + placeholderSuffix.length());
            }
        }
        return result.toString();
    }

    /**
     * 检查字符串中是否包含占位符
     * @param value 待检查的字符串
     * @return 是否包含占位符
     */
    @Override
    public boolean containsPlaceholder(String value) {
        return value != null && value.contains(placeholderPrefix) && value.contains(placeholderSuffix);
    }
}
